package srzy.driver

import srzy.diameter.PrettyPrint

object Start5 {

  def deal = {
    val ll = "3"
    val x = <r name={ ll }>{ (1 to 5).map(i => <e>{ i }</e>) }</r>
    val x2 = <r>{ (1 to 5).map(i => <e>{ i }</e>) }</r>
    val llll = new xml.PrettyPrinter(800, 2)
    val lll = x
    println(llll.format(lll))
    val lll3 = Nil ++ x
  }

  def deal1 = {
    val text = xml.XML loadFile "scenario/voice999.xml"
    val ll = (text \ "traffic" \ "send" \ "command").toList
    val x = ll.head
    import srzy.diameter._
    //println(x.toBytes.toFormatStr.split("\n").map("|B|" + _).mkString("\n"))
    //    println(x.toBytes.toDCCSeagullLog)
    val txt = x.toBytes.toDCCNode
    //    println(txt)
    val llll = new xml.PrettyPrinter(800, 2)
    println(llll.format(txt))
  }

  def deal2 = {
    val text = xml.XML loadFile "scenario/voice999.xml"
    val ll = (text \ "traffic" \ "send" \ "command").toList
    val x = ll.head
    import srzy.diameter._
    val arr = x.toBytes
    import srzy.diameter.DataFormat._
    val l = arr.toList
    val header = l.slice(0, dccHeaderLength)
    val body = l.slice(dccHeaderLength, l.length)
    //    println(BytesToSeagullLog.splitAVPs(body.toArray).map(_.toFormatStr).mkString("\n------\n"))
    val avps = BytesToSeagullLog.splitAVPs(body.toArray)
    //    println(BytesToSeagullLog.splitAVPs(avps.last.drop(12)).map(_.toFormatStr).mkString("\n------\n"))
    println(BytesToSeagullLog.avpBytesToSeagullLog(avps.last))
  }

  def deal3 = {
    val s = """|B|01 00 00 f8 00 00 01 01 00 00 00 00 00 00 00 00 
|B|00 00 00 00 00 00 01 0c 40 00 00 0c 00 00 07 d1 
|B|00 00 01 08 40 00 00 1e 74 79 64 69 63 30 2e 63 
|B|68 69 6e 61 75 6e 69 63 6f 6d 2e 63 6f 6d 00 00 
|B|00 00 01 28 40 00 00 17 63 68 69 6e 61 75 6e 69 
|B|63 6f 6d 2e 63 6f 6d 00 00 00 01 01 40 00 00 0e 
|B|00 00 82 4c 00 64 00 00 00 00 01 0a 40 00 00 0c 
|B|00 00 00 0b 00 00 01 0d 00 00 00 10 49 6e 74 65 
|B|66 61 63 65 00 00 01 16 40 00 00 0c 41 41 6e 00 
|B|00 00 01 09 40 00 00 0c 00 00 16 14 00 00 01 09 
|B|40 00 00 0c c2 a0 36 87 00 00 01 02 40 00 00 0c 
|B|00 00 00 04 00 00 01 03 40 00 00 0c 00 00 00 00 
|B|00 00 01 04 40 00 00 2c 00 00 01 0a 40 00 00 0c 
|B|00 00 00 0b 00 00 01 02 40 00 00 0c 00 00 00 04 
|B|00 00 01 03 40 00 00 0c 00 00 00 00 00 00 01 0b 
|B|00 00 00 0c 00 00 00 01 """
    val ss = s.replace("|B|", "").replaceAll("[ \n]", "")
    println(ss)
    import srzy.diameter._
    val sss = BytesToNode.dccBytesToNode(ss.hexToBytes)
    println(sss)
    val llll = new xml.PrettyPrinter(800, 2)
    println(llll.format(sss))
  }

  def deal4 = {
    val l = """<command name="CCR">
<avp name="Session-Id" value="chinaunicom.com;4.1.1"></avp>
<avp name="Session-I1" value="chinaunicom.com;">
	<avp name="Session-23d" value="chinaunic4.1.1"></avp>
	<avp name="Session-34d" value="chinaun1"></avp>
</avp>
</command>"""
    val elem = xml.XML.loadString(l)
    val ll = elem \\ "avp"
    println(ll)
    println(ll.iterator.toList.map(_.attribute("name").getOrElse("")))
    println(ll.iterator.toList.filter(_.attribute("name").getOrElse("").toString() == "Session-Id").head.attribute("value").getOrElse("").toString())
  }

  def main(args: Array[String]): Unit = {
    deal4
  }

}